
#include "./../url.hpp"
#include "./../dns_reslove.hpp"
#include "./../tcp_conn.hpp"
#include "./../http_conn.hpp"
#include "./../http_client.hpp"
#include "./../websocket_conn.hpp"


#include <iostream>

// #pragma comment(lib, "libcrypto.lib")
// #pragma comment(lib, "libssl.lib")

//#pragma comment(lib, "libeay32.lib")
//#pragma comment(lib, "ssleay32.lib")

#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "libcrypto_static.lib")
#pragma comment(lib, "libssl_static.lib")
#pragma comment(lib, "zlibwapi.lib")



void http_test()
{
	ahttp::io_service ios;

	auto tc = std::make_shared<PlainTcpConn>(ios);
	tc->async_connect("www.baidu.com", 80, nullptr);
	tc->set_connect_callback([&](TcpConnPtr c) {
		tc->close();
		});



	auto hc = std::make_shared<HttpConn>(ios);
	auto req = HttpRequest::make_request("GET", "https://www.baidu.com/", "");
	hc->Do(req, [](HttpResponePtr rsp, const ahttp::error_code& ec) {
		std::cout << rsp->http_status();
		});

	req = HttpRequest::make_request("GET", "http://www.baidu.com/", "");

	auto res = hc->sync_request(req);

	HttpClient client(ios);
	//client.set_proxy("http://127.0.0.1:1080");
	client.set_timeout(5000);
	//client.set_ippool({ "192.168.2.11" });
	// 	client.Do(req, [](HttpResponePtr rsp, const boost::system::error_code& ec) {
	// 		std::cout << rsp->http_status() << std::endl;
	// 	});

		// https://zhuanlan.zhihu.com/api/articles/38438548/recommendation?include=data%5B*%5D.article.column&limit=12&offset=0
		// chunked test https://www.huobi.br.com/-/x/pro/v1/common/exchange?symbol=ethusdt&r=l0e77o
		// no chunked https://www.okex.me/api/spot/v3/instruments
	std::string uri1 = "http://192.168.2.11:9999/falcon/scan";
	std::string uri2 = "https://www.bilibili.com/";
	auto req1 = HttpRequest::make_request("GET", uri2, "");
	client.Do(req1, [](HttpResponePtr rsp, const ahttp::error_code& ec) {
		std::cout << rsp->http_status() << std::endl;
		std::cout << rsp->payload() << std::endl;
		});

	ahttp::io_service::work wk(ios);
	ios.run();
}


void websocket_test()
{
	ahttp::io_service ctx;
	WebsocketConPtr conn = std::make_shared<WebsocketConn>(ctx);

	std::string uri = "wss://ws-feed.pro.coinbase.com";

	conn->set_proxy("http://127.0.0.1:2080");

	conn->set_close_handler([&](auto c) {
		printf("disconnected!\n");
		conn->connect(uri);
		});

	conn->set_fail_handler([&](auto c) {
		printf("failed!\n");
		conn->connect(uri);
		});

	conn->set_open_handler([&](auto c) {
		printf("connected!\n");
		std::string ss = "ETH-EUR";
		std::string msg = "{\"type\": \"subscribe\",\"channels\":[{\"name\":\"level2\",\"product_ids\": [\"" + ss + "\"]}]}";
		c->write(OPCODE::text, msg);
		});

	conn->set_message_handler([&](auto c, auto msg) {
		printf("%s\n", msg.payload().c_str());
		//c->write(awebsocket::OPCODE::text, "{\"id\": 1545910660740,\"type\" : \"subscribe\",\"topic\" : \"/contractMarket/tickerV2:\"XBTUSDT\"}");
		});

	conn->connect(uri);

	ahttp::io_service::work wk(ctx);
	ctx.run();
}

int main()
{
	//websocket_test();
	http_test();
	return 0;
}